<?php
namespace app\admin\controller\set;
use \app\admin\controller\AuthSite;
class Newsgroup extends AuthSite{
    
    
    public function open(){
        $NewsGroupManage = new \logic\set\news\NewsGroupManage($this->getUser());  
        $data = $NewsGroupManage->getList(['siteid'=> $this->siteId]);
        // $node,$pidname='pid',$pid=0,$access=null,$nid='id',$backType = 1
       // dump($data);
       // $data = \core\helper\Tools::arrayMerge($data,'pid',0,null,'id',2);
        $data = \core\helper\Tools::arrayMerge($data);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    
    public function add(){
        try{
            if( request()->post() ){            
                $save = [
                    'pid'        => input('pid',0),
                    'groupname'  => input('name'),
                    'siteid'     => $this->siteId
                ];
                $NewsGroupManage = new \logic\set\news\NewsGroupManage($this->getUser());  
                $NewsGroupManage->add($save);                
                $data = $NewsGroupManage->getTreeList(['siteid'=> $this->siteId]);                
                return $this->success('添加成功','/set/newsgroup/index',$data);
            }  
        }catch( \think\Exception $e){
            $this->error( $e->getMessage() );
        }   
    }
    
    public function edit(){
        try{            
            if( request()->post() ){            
                $save = input('data');
                $NewsGroupManage = new \logic\set\news\NewsGroupManage($this->getUser());  
                $NewsGroupManage->editAll($save,$this->siteId);                     
                return $this->success('修改成功','/set/newsgroup/index',$data);
            }
        }catch( \think\Exception $e){
            $this->error( $e->getMessage());
        }
    }
    
    /**
     * 修改状态
     */
    public function editstatus(){
        try{
            $id = input('id');
            $NewsGroupManage = new \logic\set\news\NewsGroupManage($this->getUser()); 
            $result = $NewsGroupManage->editStatus($id,$this->siteId );
            $list = $NewsGroupManage->getTreeList(['sitid'=>$this->siteId]);
            return $this->success('成功','',$list);
        }catch(\think\Exception $e){
            return $this->error($e->getMessage());
        }
    }
    
    /**
     * 获取数据
     */
    public function ajaxlist(){
        try{
            $NewsGroupManage = new \logic\set\news\NewsGroupManage($this->getUser()); 
            $list = $NewsGroupManage->getTreeList(['sitid'=>$this->siteId]);
            return $this->success('成功','',$list);
        }catch(\think\Exception $e){
            return $this->error($e->getMessage());
        }
    }
}